package com.yyx.lgb.manager.task;

import cn.hutool.core.date.DateUtil;
import com.yyx.lgb.manager.mapper.OrderInfoMapper;
import com.yyx.lgb.manager.mapper.OrderStatisticsMapper;
import com.yyx.lgb.model.entity.order.OrderStatistics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ClassName: OrderStatisticsTask
 * Package: com.yyx.lgb.manager.task
 * Description:
 *
 * @Author: yyx
 * @Create: 2025/8/8 - 16:10
 */
@Component
@Slf4j
public class OrderStatisticsTask {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    //每天凌晨2点，查询前一天统计数据，把统计之后数据添加统计结果表里面
    //@Scheduled(cron = "0 0 2 * * ?")
    @Scheduled(cron = "0/10 * * * * ?")   //TODO   为了测试
    public void orderTotalAmountStatistics() {
        //获取前一天日期
        String createTime = DateUtil.offsetDay(new Date(), -1).toString(new SimpleDateFormat("yyyy-MM-dd"));
        //2根据前一天日期进行统计功能，(分组操作)
        // 统计前一天交易金额
        OrderStatistics orderStatistics = orderInfoMapper.selectOrderStatistics(createTime);
        //3把统计之后的数据，添加统计结果表里面
        if(orderStatistics != null) {
            orderStatisticsMapper.insert(orderStatistics) ;
        }
    }

}
